package com.css.bjdt_core.reportPrepare.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.report.entity.RptTemplate;
import com.css.bjdt_core.report.service.ReportTemplateManageService;
import com.css.bjdt_core.reportPrepare.dao.RptLssuedAreasInfoDao;
import com.css.bjdt_core.reportPrepare.entity.RptLssuedAreasInfo;
import com.css.bjdt_core.reportPrepare.entity.RptLssuedAreasTemplateRel;
import com.css.bjdt_core.reportPrepare.entity.ViewRptGroupTemplate;
import com.css.bjdt_core.util.CommonUtil;

@Service
public class RptLssuedAreasCloneService {

	@Autowired
	private RptLssuedAreasInfoDao rptLssuedAreasInfoDao;

	@Autowired
	private ReportTemplateManageService reportTemplateManageService;

	public void rptLssuedAreasClone(String dzzId, String annualId, List<String> areaIdsList) {
		for (String areaId : areaIdsList) {
			//先做准备工作，根据areaId找到区域信息，再根据区域信息找到对应的报表，
			//然后根据对应的报表名称和传入的版本ID找到对应的新版本的报表ID，扔到LIST下面
			List<String> templateNewIds = new ArrayList<String>();
			RptLssuedAreasInfo rptLssuedAreasInfo = rptLssuedAreasInfoDao.getARptLssuedAreasInfoById(areaId);
			List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelList = rptLssuedAreasInfoDao
					.getAllTemplateIdByAreaId(areaId);
			for (RptLssuedAreasTemplateRel rptLssuedAreasTemplateRel : rptLssuedAreasTemplateRelList) {
				RptTemplate rptTemplate = reportTemplateManageService
						.getAnRptTemplate(rptLssuedAreasTemplateRel.getTemplateId());
				int beginNum = rptTemplate.getName().replace(" ", "").indexOf("表") + 1;
				int endNum = rptTemplate.getName().replace(" ", "").length();
				String newTemplateNew = rptTemplate.getName().replace(" ", "").substring(beginNum, endNum);
				ViewRptGroupTemplate viewRptGroupTemplate = reportTemplateManageService
						.getAViewRptGroupTemplateByName(newTemplateNew, annualId);
				if (null != viewRptGroupTemplate) {
					templateNewIds.add(viewRptGroupTemplate.getTemplateId());
				}
			}

			//开始保存区域信息,区域对应的报表信息
			String areaNewId = CommonUtil.getUUID();
			rptLssuedAreasInfo.setAreaId(areaNewId);
			rptLssuedAreasInfo.setAnnualId(annualId);
			rptLssuedAreasInfoDao.saveARptLssuedAreasInfo(rptLssuedAreasInfo);
			List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelNewList = new ArrayList<RptLssuedAreasTemplateRel>();
			for (String templateNewId : templateNewIds) {
				RptLssuedAreasTemplateRel rptLssuedAreasTemplateRel = new RptLssuedAreasTemplateRel();
				rptLssuedAreasTemplateRel.setAreaId(areaNewId);
				rptLssuedAreasTemplateRel.setTemplateId(templateNewId);
				rptLssuedAreasTemplateRelNewList.add(rptLssuedAreasTemplateRel);
			}
			rptLssuedAreasInfoDao.saveRptLssuedAreasTemplateRelList(rptLssuedAreasTemplateRelNewList, areaNewId);
		}

	}
}
